package com.yanqu.road.server.manger.activity.dafuweng.event;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWEvent;
import com.yanqu.road.entity.activity.dafuweng.DFWUserSecretRoad;
import com.yanqu.road.server.manger.activity.dafuweng.DaFuWengMgr;
import com.yanqu.road.server.manger.activity.dafuweng.building.BaseDFWBuilding;
import com.yanqu.road.server.manger.activity.dafuweng.entiety.SecretRoadReward;
import com.yanqu.road.utils.RandomGoodInfo;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @Author wpj
 * @Date 2021/1/7
 */
public class DFWSecretEvent extends BaseDFWEvent {


    public DFWSecretEvent(BaseDFWBuilding baseDFWBuilding, DFWEvent dfwEvent) {
        super(baseDFWBuilding, dfwEvent);
    }

    @Override
    public void doEvent() {
        userData.setEventId(dfwEvent.getEventId());
        DFWConfig dfwConfig = DaFuWengMgr.getDfwConfig();
        int stepNum = dfwEvent.getParamList().get(0);   //格子数量
        //以起始格子和密道格子数量为准
        Random random = new Random();
        List<Integer> weightList = new ArrayList<>();
        String[] weights = dfwConfig.getSecretBigRewardNumWeight().split("\\|");
        for (String weight : weights) {
            weightList.add(Integer.valueOf(weight.split(";")[1]));
        }
        int index = RandomHelper.getRandomIndexByWeight(weightList, random);
        //稀有奖励个数
        int bigRewardNum = Integer.valueOf(weights[index].split(";")[0]);
        List<SecretRoadReward> rewardList = new ArrayList<>();
        //随机稀有奖励
        for (int i = 0; i < bigRewardNum; i++) {
            rewardList.add(new SecretRoadReward(true, getRandomRewardProperty(dfwConfig.getSecretBigRewardPool(), 1, true, random)));
        }
        //随机普通奖励
        for (int i = 0; i < stepNum - bigRewardNum; i++) {
            rewardList.add(new SecretRoadReward(false, getRandomRewardProperty(dfwConfig.getSecretNormalRewardPool(), 1, true, random)));
        }
        //打乱顺序
        Collections.shuffle(rewardList);
        List<DFWUserSecretRoad> secretRoads = new ArrayList<>();
        int i = 0;
        for (SecretRoadReward reward : rewardList) {
            DFWUserSecretRoad dfwUserSecretRoad = new DFWUserSecretRoad();
            dfwUserSecretRoad.setBigReward(reward.isBigReward());
            dfwUserSecretRoad.setPositionIndex(i);
            dfwUserSecretRoad.setReward(PropertyHelper.parsePropertyToString(reward.getReward()));
            dfwUserSecretRoad.setGetReward(false);
            i++;
            secretRoads.add(dfwUserSecretRoad);
        }
        userData.setUserSecretRoadList(secretRoads);
        userData.setExpandParam("-1");
        userData.setComplete(false);
    }

    /**
     * 随机抽取奖励  返回Property
     *
     * @param rewardStr (道具ID=数量;概率|道具ID=数量;概率)
     * @param num      随机次数
     * @param isRandom 是否放回
     * @return
     */
    public static Property getRandomRewardProperty(String rewardStr, int num, boolean isRandom, Random random) {
        List<RandomGoodInfo> dropList = new ArrayList<>();
        for (String str : rewardStr.split("\\|")) {
            dropList.add(new RandomGoodInfo(str));
        }
        Property property = new Property();
        List<RandomGoodInfo> temList = new ArrayList<>(dropList);
        int size = temList.size();
        if ((isRandom && num <= 0 && size == 0) || (!isRandom && size < num)) {
            return property;
        }
        int totalProb = 0;
        for (int i = 0; i < size; i++) {
            totalProb += temList.get(i).getPower();
        }

        for (int j = num; j > 0; j--) {//j表示要取第几个
            int luckNum = random.nextInt(totalProb) + 1;
            int curSize = temList.size();
            for (int k = 0, curProb = 0; k < curSize; k++) {
                RandomGoodInfo randomGoodInfo = temList.get(k);
                curProb += randomGoodInfo.getPower();
                if (curProb >= luckNum) {
                    property.addProperty(randomGoodInfo.getProperty());

                    if (!isRandom) {
                        totalProb -= randomGoodInfo.getPower();
                        temList.remove(k);
                    }
                    break;
                }
            }
        }
        return property;
    }
}
